package org.haredot.controller;

import org.haredot.annotation.LogModule;
import org.haredot.dto.DepartDTO;
import org.haredot.entity.Depart;
import org.haredot.entity.User;
import org.haredot.service.DepartService;
import org.haredot.vo.DepartVo;
import org.haredot.vo.PageVo;
import org.haredot.vo.ResultVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@LogModule("部门管理")
@RestController
@RequestMapping("/depart")
public class DepartController {

    @Resource
    private DepartService departService ;

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('depart:view:add-one', 'depart:view:add-child')")
    public ResultVo<Depart> addDepart(@RequestBody DepartVo departVo) {
        return departService.saveDepart(departVo) ;
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('depart:view')")
    public ResultVo<PageVo<DepartDTO>> list(@RequestParam("page") Integer page ,
                                            @RequestParam("limit") Integer limit) {
        // 构建一个 分页对象
        PageVo<DepartDTO> objectPageVo = new PageVo<>(page, limit);

        // 调用业务逻辑层，处理业务查询数据
        return departService.queryDeparts(objectPageVo);
    }

    @PutMapping("/modify{id}")
    @PreAuthorize("hasAuthority('depart:view:modify')")
    public ResultVo<?> modifyDepart(@PathVariable("id")Long id, @RequestBody Depart depart) {

        // 将 主键 ID 放到 depart 中
        depart.setId(id);

        return departService.modifyDepart(depart);
    }

    @DeleteMapping("/remove/{departId}")
    @PreAuthorize("hasAuthority('depart:view:remove')")
    public ResultVo<?> removeDepart(@PathVariable("departId") Long departId) {

        return departService.removeDepart(departId) ;
    }

    @GetMapping("/users/{departId}")
    @PreAuthorize("hasAuthority('depart:view:query-user')")
    public ResultVo<?>  userList(@PathVariable("departId")Long departId,
                                 @RequestParam("page") Integer page, @RequestParam("limits")Integer limits) {

        PageVo<User> pageVo = new PageVo<User>(page, limits);

        return departService.queryUsers(departId, pageVo);
    }
}
